﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using NetCore.Configuration;

namespace NetCore.Services
{
    /// <summary>
    /// 读取配置文件帮助类 初始化时调用Init静态方法初始化静态变量
    /// </summary>
    public class CustomConfigurationManager
    {
        //private readonly IConfigurationRoot _appConfiguration;
        /// <summary>
        /// 读配置文件IConfiguration对象
        /// </summary>
        public static IConfiguration Configuration { get; set; }
        /// <summary>
        /// 当前根目录
        /// </summary>
        public static string RootPath { get; set; }
        /// <summary>
        /// 网站根目录
        /// </summary>
        public static string WebRootPath { get; set; }
        /// <summary>
        /// 枚举类型所在程序集名 EnumAssemblyName
        /// </summary>
        public static string EnumAssemblyName { get; set; }

        /// <summary>
        /// 读取配置文件初始化系统静态变量,模块启动时运行，
        /// </summary>
        /// <param name="env"></param>
        public static void Init(IHostingEnvironment env)
        {
            //_appConfiguration = AppConfigurations.Get(env.ContentRootPath, env.EnvironmentName, env.IsDevelopment());
            Configuration = AppConfigurations.Get(env.ContentRootPath, env.EnvironmentName, env.IsDevelopment());
            RootPath = env.ContentRootPath;
            WebRootPath = env.WebRootPath;
            EnumAssemblyName = Configuration["Abp:EnumAssemblyName"] ?? typeof(CustomConfigurationManager).Assembly.GetName().Name;
        }

        static CustomConfigurationManager()
        {
            #region 获取Configuration

            //var path1 = Directory.GetCurrentDirectory();//获取应用程序根目录 待测试
            //var path = AppContext.BaseDirectory;//获取bin目录路径
            //var name = typeof(T).GetTypeInfo().Assembly.GetName().Name;//获取程序集名称
            //var version = Assembly.GetEntryAssembly().GetName().Version.ToString(); //获取程序集版本

            ////自定义json文件路径：根据bin目录路径来拼需要的类库的json文件路劲
            //var baseDir = AppContext.BaseDirectory;
            //if (!File.Exists(baseDir + "/appsettings.json"))
            //{
            //    DirectoryInfo di = new DirectoryInfo($"{baseDir}../../../");//
            //    baseDir = di.FullName;
            //    if (!File.Exists(baseDir + "appsettings.json")) throw new Exception("没有找到配置文件");
            //}
            //Configuration = new ConfigurationBuilder()
            //    .SetBasePath(RootPath)//path
            //    .Add(new JsonConfigurationSource { Path = "appsettings.json", Optional = false, ReloadOnChange = true })
            //    .AddEnvironmentVariables()
            //    .Build();

            #endregion

        }

        /// <summary>
        /// 读取配置文件某节点下的json转换为指定类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetAppSettings<T>(string key) where T : class, new()
        {
            var section = Configuration.GetSection(key);
            var appconfig = new ServiceCollection()
                .AddOptions()
                .Configure<T>(section.Bind)
                .BuildServiceProvider()
                .GetService<IOptions<T>>()
                .Value;
            return appconfig;
        }
    }

}
